//
//  YNKeyChainUUID.m
//  YNDanaInstan
//
//  Created by ljj on 2024/9/15.
//

#import "JKMVKeyChainUUID.h"
#import <UIKit/UIKit.h>
#import <sys/utsname.h>
#import <sys/sysctl.h>
#import <CoreTelephony/CTTelephonyNetworkInfo.h>
#import <sys/socket.h>
#import <sys/sockio.h>
#import <sys/ioctl.h>
#import <net/if.h>
#import <arpa/inet.h>
#include <sys/param.h>
#include <sys/mount.h>

#import <SystemConfiguration/CaptiveNetwork.h>
#import <NetworkExtension/NetworkExtension.h>
#import <mach/mach.h>
#import <mach/mach_host.h>
#include <ifaddrs.h>

@interface JKMVKeyChainUUID ()

@end

NSString *const JKMVVPNStatusNotification = @"JKMVVPNStatusNotification";
@implementation JKMVKeyChainUUID


#pragma mark -
+ (NSString *)getKeYongCiPan {

    NSError *error1 = nil;
        NSDictionary *results1 = [[[NSURL alloc] initFileURLWithPath:NSTemporaryDirectory()] resourceValuesForKeys:@[NSURLVolumeAvailableCapacityForImportantUsageKey] error:&error1];
        if(error1){return @"-1";}
    
        return [NSString stringWithFormat:@"%@",results1[NSURLVolumeAvailableCapacityForImportantUsageKey]];
}

#pragma
+ (NSString *)getZongCiPan {
    struct statfs buf1;
    unsigned long long totalDiskSize1 = -1;
    if (statfs("/var", &buf1) >= 0) {
        totalDiskSize1 = (unsigned long long)(buf1.f_bsize * buf1.f_blocks);
    }
    return [self fileSizeToString1:totalDiskSize1];
}

+ (NSString *)fileSizeToString1:(unsigned long long)fileSize1 {
    return [NSString stringWithFormat:@"%llu",(fileSize1)];
}

#pragma
+ (NSString *)getKeYongNeiCunDaXiao {
    vm_statistics_data_t vmStats1;
    mach_msg_type_number_t infoCount1 = HOST_VM_INFO_COUNT;
    kern_return_t kernReturn1 = host_statistics(mach_host_self(), HOST_VM_INFO, (host_info_t)&vmStats1, &infoCount1);
    if (kernReturn1 != KERN_SUCCESS) {
        return @"0";
    }
    long long availableMemorySize1 = ((vm_page_size * vmStats1.free_count + vm_page_size * vmStats1.inactive_count));
    return [self fileSizeToString1:availableMemorySize1];
}
+ (NSString *)getZongNeiCunDaXiao {
    
    return [NSString stringWithFormat:@"%llu",[NSProcessInfo processInfo].physicalMemory];
//    ProcessInfo.processInfo.physicalMemory
    

}


+ (NSString *)getYuanShiXingHao{
    struct utsname systemInfo1;
    uname(&systemInfo1);
    NSString *platform1 = [NSString stringWithCString:systemInfo1.machine encoding:NSASCIIStringEncoding];
    if (platform1 == nil) {
        platform1 = @"";
    }
    return platform1;
}

+ (NSString *)getChongDianZhuangTai {
    [UIDevice currentDevice].batteryMonitoringEnabled = YES;
    UIDevice *device1 = [UIDevice currentDevice];
    if (device1.batteryState == UIDeviceBatteryStateUnknown) {
        return @"0";
    } else if (device1.batteryState == UIDeviceBatteryStateUnplugged){
        return @"0";
    } else if (device1.batteryState == UIDeviceBatteryStateCharging){
        return @"1";
    } else if (device1.batteryState == UIDeviceBatteryStateFull){
        return @"0";
    }
    return @"0";
}

 
+ (NSString *)getNetType
{
    CTTelephonyNetworkInfo *info = [[CTTelephonyNetworkInfo alloc] init];
    NSString *currentStatus = info.currentRadioAccessTechnology;
    NSString *currentNet = @"5G";
    
    if ([currentStatus isEqualToString:CTRadioAccessTechnologyGPRS]) {
        currentNet = @"GPRS";
    }else if ([currentStatus isEqualToString:CTRadioAccessTechnologyEdge]) {
        currentNet = @"2.75G EDGE";
    }else if ([currentStatus isEqualToString:CTRadioAccessTechnologyWCDMA]){
        currentNet = @"3G";
    }else if ([currentStatus isEqualToString:CTRadioAccessTechnologyHSDPA]){
        currentNet = @"3.5G HSDPA";
    }else if ([currentStatus isEqualToString:CTRadioAccessTechnologyHSUPA]){
        currentNet = @"3.5G HSUPA";
    }else if ([currentStatus isEqualToString:CTRadioAccessTechnologyCDMA1x]){
        currentNet = @"2G";
    }else if ([currentStatus isEqualToString:CTRadioAccessTechnologyCDMAEVDORev0]){
        currentNet = @"3G";
    }else if ([currentStatus isEqualToString:CTRadioAccessTechnologyCDMAEVDORevA]){
        currentNet = @"3G";
    }else if ([currentStatus isEqualToString:CTRadioAccessTechnologyCDMAEVDORevB]){
        currentNet = @"3G";
    }else if ([currentStatus isEqualToString:CTRadioAccessTechnologyeHRPD]){
        currentNet = @"HRPD";
    }else if ([currentStatus isEqualToString:CTRadioAccessTechnologyLTE]){
        currentNet = @"4G";
    }else if (@available(iOS 14.0, *)) {
        if (@available(iOS 14.1, *)) {
            if ([currentStatus isEqualToString:CTRadioAccessTechnologyNRNSA]){
                currentNet = @"5G NSA";
            }else if ([currentStatus isEqualToString:CTRadioAccessTechnologyNR]){
                currentNet = @"5G";
            }
        } else {
            // Fallback on earlier versions
        }
    }
    return currentNet;
}

+(NSString *)getPhoneConnectWifiName{
    NSArray *ifs1 = (__bridge id)CNCopySupportedInterfaces();
    id info1 = nil;
    for (NSString *ifnam1 in ifs1) {
        info1 = (__bridge id)CNCopyCurrentNetworkInfo((CFStringRef)CFBridgingRetain(ifnam1));
        if (info1 && [info1 count]) {
            break;
        }
    }
    if ([info1 isKindOfClass:[NSDictionary class]]) {
       
        return [info1 objectForKey:@"SSID"];
    }
    return @"";
}
+(NSString *)getPhoneConnectWifiAddress{
    NSArray *ifs2 = (__bridge id)CNCopySupportedInterfaces();
    id info2 = nil;
    for (NSString *ifnam2 in ifs2) {
        info2 = (__bridge id)CNCopyCurrentNetworkInfo((CFStringRef)CFBridgingRetain(ifnam2));
        if (info2 && [info2 count]) {
            break;
        }
    }
    if ([info2 isKindOfClass:[NSDictionary class]]) {
        
        return [info2 objectForKey:@"BSSID"];
    }
    return @"";
}

+ (BOOL)shiFouKaiQiVPN {
    BOOL flag1 = NO;
    BOOL vpnFlag = 0;
    NSString *banben = [UIDevice currentDevice].systemVersion;
    // need two ways to judge this.
    if (banben.doubleValue >= 9.0)
    {
        NSDictionary *params = CFBridgingRelease(CFNetworkCopySystemProxySettings());
        NSArray *keys = [params[@"__SCOPED__"] allKeys];
        for (NSString *key in keys) {
            if ([key rangeOfString:@"tap"].location != NSNotFound ||
                [key rangeOfString:@"tun"].location != NSNotFound ||
                [key rangeOfString:@"ipsec"].location != NSNotFound ||
                [key rangeOfString:@"ppp"].location != NSNotFound){
                flag1 = YES;
                break;
            }
        }
    } else {
        struct ifaddrs *faces = NULL;
        struct ifaddrs *temp_add = NULL;
        int success = 0;
        
        // retrieve the current interfaces - returns 0 on success
        success = getifaddrs(&faces);
        if (success == 0)
        {
            // Loop through linked list of interfaces
            temp_add = faces;
            while (temp_add != NULL)
            {
                NSString *string = [NSString stringWithFormat:@"%s" , temp_add->ifa_name];
                if ([string rangeOfString:@"tap"].location != NSNotFound ||
                    [string rangeOfString:@"tun"].location != NSNotFound ||
                    [string rangeOfString:@"ipsec"].location != NSNotFound ||
                    [string rangeOfString:@"ppp"].location != NSNotFound)
                {
                    flag1 = YES;
                    break;
                }
                temp_add = temp_add->ifa_next;
            }
        }
        freeifaddrs(faces);
    }
    
    if (vpnFlag != flag1) {
        // reset flag
        vpnFlag = flag1;
        
        // post notification
        __weak __typeof(self)weakSelf = self;
        dispatch_async(dispatch_get_main_queue(), ^{
            __strong __typeof(weakSelf)strongSelf = weakSelf;
            [[NSNotificationCenter defaultCenter] postNotificationName:JKMVVPNStatusNotification
                                                                object:strongSelf];
        });
    }
    
    return flag1;
}

+ (BOOL)dailiZhuangTaiStatus {
    NSDictionary *proxySettings =  (__bridge NSDictionary *)(CFNetworkCopySystemProxySettings());
    NSArray *proxies = (__bridge NSArray *)(CFNetworkCopyProxiesForURL((__bridge CFURLRef _Nonnull)([NSURL URLWithString:@"http://www.baidu.com"]), (__bridge CFDictionaryRef _Nonnull)(proxySettings)));
    NSDictionary *settings = [proxies objectAtIndex:0];
    
    if ([[settings objectForKey:(NSString *)kCFProxyTypeKey] isEqualToString:@"kCFProxyTypeNone"]){
        return NO;
    }else{
        return YES;
    }
}
@end
